from channels import Group
from main_processing.main_process import MainProcess
from danmu_filter.main_filter import DanmuFilter
import re
import os
url="https://www.douyu.com/2959887"
channel_list=[]
danmu_filter=DanmuFilter()
mainProcess = None
danmu_filter.loads(path='danmu_filter/dirty_dict.txt')# 相对于manage.py来说 python 相对路径
def danmu_ws_connect(message):
    message.reply_channel.send({"accept":True})
    message.reply_channel.send({"text":"server connecting...."+message.reply_channel.__dict__['name']})
    Group("danmu_resolution").add(message.reply_channel)

def danmu_ws_disconnect(message):
    Group("danmu_resolution").discard(message.reply_channel)
    global channel_list
    channel_json={}
    for channel_json in channel_list:
        if channel_json['reply_channel'].__dict__['name'] == message.reply_channel.__dict__['name']:
            is_in = True;
            break
    if is_in is not True:
        message.reply_channel.send({"text": "I don't know why!"})
    else:
        if channel_json["process"].is_alive():
            channel_json["process"].terminate()
            print("success terminate..............................................")
        channel_list.remove(channel_json)


def danmu_ws_start(message):
    is_in=False
    channel_json={}
    global danmu_filter, url,channel_list
    if message['text'] == "danmu_ws transferring":
        for channel_json in channel_list:
            if channel_json['reply_channel'].__dict__ ['name']== message.reply_channel.__dict__['name']:
                is_in = True;
                break
        if is_in is not True:
            message.reply_channel.send({"text":"Please register before"+str(len(channel_list))+channel_json['reply_channel'].__dict__ ['name']+";;;;;;"+message.reply_channel.__dict__['name']})
        else:
            danmu_filter.interface_filter(channel_json["process"].lock1, channel_json["process"].lock2, channel_json["reply_channel"],id(channel_json["reply_channel"]))


    #global mainProcess,danmu_filter,url
    #print(message['text'])


    elif re.search(pattern='https://www.douyu.com',string=message['text']) is not None:
         url = message['text']
         for channel_json in channel_list:
             if channel_json['reply_channel'].__dict__['name'] == message.reply_channel.__dict__['name']:
                 is_in = True
                 break
         if is_in is not True:
             channel_json_add={}
             channel_json_add["reply_channel"] = message.reply_channel
             channel_json_add["process"] = MainProcess(url=url,id=id(channel_json_add['reply_channel']))
             channel_json_add["process"].Main_start()
             channel_list.append(channel_json_add)
         else:
             if channel_json["process"].is_alive():
                 channel_json["process"].terminate()
                 print("success terminate..............................................")
             channel_json["process"]=MainProcess(url=url,id=id(channel_json['reply_channel']))
             channel_json["process"].Main_start()
         message.reply_channel.send({'text': 'url set ok'+message.reply_channel.__dict__['name']})
    else:
         message.reply_channel.send({'text':'error messages'})